package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Teacher;
import com.rk.admin.service.TeacherService;
import com.rk.admin.tools.CommonResult;
import com.rk.admin.tools.SysConstant;
import com.rk.admin.utils.FileNameUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "师资队伍")
@RestController
@CrossOrigin //前后端分离配置跨域访问
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @Value("${environment.system}")
    private String system;


    /**
     * 查询所有教师信息(展示教师姓名)
     */
    @ApiOperation("查询所有教师信息(展示教师姓名)")
    @GetMapping("/findAll")
    public CommonResult findallTeacher(){
        return  CommonResult.success(teacherService.findallTeacher());
    }


    /**
     * 通过id查询教师信息
     */
    @ApiOperation("通过id查询教师信息")
    @GetMapping("/findbyid")
    public CommonResult findbyid(@RequestParam("id") Integer id){
        List<Teacher> list=new ArrayList<>();
        list.add(teacherService.findbyid(id));
        return  CommonResult.success(list);
    }

    /**
     * 通过id删除教师信息
     */
    @ApiOperation("通过id删除")
    @SystemControllerLog(description ="删除教师")
    @PostMapping("/deletebyid")
    public CommonResult deletebyid(@RequestParam("id") Integer id){
        Boolean flag = teacherService.deleteById(id);
        if(flag){//删除成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("删除失败!");
        }
    }



    @ApiOperation("头像上传")
    @PostMapping("/uploadHeadImg")
    @SystemControllerLog(description ="上传教师头像")
    public CommonResult uploadteacherImg(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String savefileName="";
        if (!file.isEmpty()) {//如果上传的头像不为空
            //存储头像到tecaherHeadImg目录下
            //获取文件原来的名字
            //获取文件类型
            String fileType = FileNameUtil.getFileType(originalFilename);
            //更改文件存储的名称，避免上传的文件名重复而覆盖
            savefileName =  FileNameUtil.getUUIDFileName() + fileType;


            String savePath="";

            String path = System.getProperty("user.dir");

            if("linux".equals(system)){
                savePath= SysConstant.TEACHERHEAD_LINUX;
            }else if("windows".equals(system)){
                savePath = path+ SysConstant.TEACHERHEAD_WINDOWS;
            }

            //头像存储路径
            File realpath=new File(savePath);
            //如果路径不存在，就创建路径，上传到服务器部署的时候，肯定没有这个路径存在的
            if(!realpath.exists())
            {
                realpath.mkdirs();
            }

            //文件转存
            try {
                file.transferTo(new File(realpath.getAbsolutePath() + File.separator + savefileName));
                System.out.println("存储路径:" + realpath.getAbsolutePath() + File.separator + savefileName);
                return CommonResult.success("/teacherImg/" + savefileName, "头像上传成功!");
            } catch (IOException e) {
                e.printStackTrace();
                return CommonResult.failed("上传失败!");
            }
        }
        return CommonResult.success("/teacherImg/" + savefileName, "头像上传成功!");

    }


    /**
     * 添加教师信息
     */
    @SystemControllerLog(description ="添加教师")
    @ApiOperation("添加教师")
    @PostMapping("/add")
    public CommonResult add(@RequestBody Teacher teacher){
        Boolean flag = teacherService.add(teacher);
        if(flag){//如果添加成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("添加失败!");
        }
    }

    /**
     * 修改教师信息
     */
    @SystemControllerLog(description ="修改教师信息")
    @ApiOperation("通过id修改教师信息")
    @PostMapping("/updatebyid")
    public CommonResult updatebyid(@RequestBody Teacher teacher){
        Boolean flag = teacherService.updatebyid(teacher);
        if(flag){//如果修改成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("修改失败!");
        }
    }



    @ApiOperation(("分页查询教师信息"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页数据条数",required = true,example = "5"),
            @ApiImplicitParam(name = "name",value = "教师姓名",example = "罗某"),
    })
    @GetMapping("/findPage")
    public CommonResult findPage(Integer page,Integer limit, String name){
        return CommonResult.success(teacherService.selectPage(page,limit,name));
    }

}
